/**
 * @author KujoMiva
 * @name term.js
 * @desc 类型&分类状态树
 * @time 2020年06月12日 15:55:18 星期五
 */
import { getHeadMenuAPI } from '@/api/app';
// 类型对应的路由地址
const typePath = { 2: '/audio', 3: '/image', 4: '/article', 5: '/game', 6: '/video' };
// 整理请求获取的菜单数据
function formatHeadMenu(data) {
    const arr = data.reduce((pv, v) => {
        const { name, num = 0, cid = 0, type, child } = v;
        if (name === '基本类型') return pv;
        const to = cid ? `${typePath[type]}/classify/${cid}` : typePath[type];
        const pushData = { to, name, num };
        if (child) pushData.child = formatHeadMenu(child);
        pv.push(pushData);
        return pv;
    }, []);
    return arr;
}

export default {
    namespaced: true,
    state: {
        requestState: false,
        headMenuData: [
            { to: '/', name: '首页', icon: 'icon-shouye1' },
        ],
    },
    mutations: {
        mixinHeadMenuData(state, data) {
            const asyncMenuData = formatHeadMenu(data);
            state.headMenuData = [...state.headMenuData, ...asyncMenuData];
            state.requestState = true;
        },
    },
    actions: {
        async getHeadMenuAction({ state, commit }) { // 发请求查询所有分类
            if (state.requestState) return;
            const { data } = await getHeadMenuAPI();
            commit('mixinHeadMenuData', data);
        },
    },
};
